<?php

namespace App\Services;

use App\Models\IntegralLog;
use App\Models\IntegralSet;
use App\Models\Store;
use App\Models\User;
use Illuminate\Support\Facades\DB;

/**
 * 日志服务支持
 * Class IntegralLogService
 * @package App\Services
 */
class IntegralLogService extends BaseService
{

    //日志模型
    public $model;
    public $UserService;

    public function __construct()
    {
        $this->model = new IntegralLog();
        $this->UserService = new UserService();
    }


    /**
     * 2021/3/15--22:29
     * @return mixed
     * @author:董宗澳
     * @email:2453611300
     * @wechat:DZA74941
     * @QQ:2453611300
     * @web:www.quzha.net
     * @method:getLogList
     * @effect:积分列表
     */
    public static function getLogList($is_paginate = true)
    {
        $list = IntegralLog::where(function ($query) {

            //状态
            if (isset(request()->set_id) && request()->set_id != 0) {
                $query->where('integral_set_id', request()->set_id);
            }
            // 日志类型 type 1 减去 2 =增加
            if (isset(request()->type) && request()->type != 0) {
                $query->where('type', request()->type);
            }

        });
        $current_total_integral = $list->sum('integral');

        //当前总积分
        $total_integral = $list->sum('integral');
        if (isset(request()->year) && isset(request()->month)) {
            $year = request()->year ?? 2020;
            $month = request()->month ?? 1;
            $start = now()->year($year)->month($month)->firstOfMonth()->timestamp;
            $end = now()->year($year)->month($month)->lastOfMonth()->timestamp;
            $list->whereBetween('created_at', [$start, $end]);
        } else {
            $start = now()->firstOfMonth()->timestamp;
            $end = now()->lastOfMonth()->timestamp;
            $list->whereBetween('created_at', [$start, $end]);
        }
        if ($is_paginate) {
            $list = $list->orderBy('created_at', 'desc')->paginate(request()->per_page ?? 30)->toArray();
        } else {
            $userService = new UserService();
            $id = $userService->getUserInfo()['id'];
            $list->where('user_id', $id);

            //当前总积分
            $current_total_integral = $list->sum('integral');
            $list = $list->orderBy('created_at', 'desc')->with(
                [
                    'order' => function ($q) {
                        return $q->select('id', 'user_id', 'store_id', 'order_name', 'order_image', 'total_price', 'order_no');
                    },
                    'order_goods' => function ($q) {
                        return $q;
                    },
                ]
            )->get()->toArray();
        }
        if (!empty($list['data'])) {
            foreach ($list['data'] as $key => $v) {
                // 用户信息
                $list['data'][$key]['username'] = User::where('id', $v['user_id'])->value('username');
                $set_info = IntegralSet::find($v['integral_set_id']);
                if ($set_info['type'] == 1) {
                    $list['data'][$key]['type_text'] = '商家分类';
                    $list['data'][$key]['belong_text'] = DB::table('shop_stores_cate')->where('id', $set_info['belong_id'])->value('cate_name');
                } else {
                    $list['data'][$key]['belong_text'] = Store::where('id', $set_info['belong_id'])->value('store_name');
                    $list['data'][$key]['type_text'] = '商家';
                }
            }
        }

        $data['list'] = $list;

        $data['total_integral'] = $total_integral;
        $data['current_total_integral'] = $current_total_integral;
        return $data;
    }

    /**
     * 2021/3/15--22:28
     * @author:董宗澳
     * @email:2453611300
     * @wechat:DZA74941
     * @QQ:2453611300
     * @web:www.quzha.net
     * @method:getLogInfo
     * @effect:获取日志详情
     */
    public function getLogInfo($id)
    {
        return $this->model->where('id', $id)->with(
            [
                'order' => function ($q) {
                    return $q->select('id', 'user_id', 'store_id', 'order_name', 'order_image', 'total_price', 'order_no');
                },
                'order_goods' => function ($q) {
                    return $q;
                },
                'store' => function ($q) {
                    return $q->select('id', 'store_name', 'store_logo', 'store_mobile');
                },
                'user' => function ($q) {
                    return $q->select('id', 'username', 'nickname', 'phone');
                }
            ]
        )->first();
    }

    /**
     * 2021/3/15--22:28
     * @author:董宗澳
     * @email:2453611300
     * @wechat:DZA74941
     * @QQ:2453611300
     * @web:www.quzha.net
     * @method:setLog
     * @effect:设置日志
     */
    public function setLog($user_id, $reward, $store_id, $integral_set_id, $type, $integral, $form_order_id)
    {
        //积分变化前
        $before_integral = $this->UserService->getUserInfoFiled('integral', $user_id);
        // 积分变化后
        if ($type == 1) {
            $after_integral = ($before_integral += $integral);
        } else {
            $after_integral = ($before_integral -= $integral);
        }
        $insert = [
            'user_id' => $user_id,
            'reward' => $reward,
            'store_id' => $store_id,
            'integral' => $integral,
            'integral_set_id' => $integral_set_id,
            'before_integral' => $this->UserService->getUserInfoFiled('integral', $user_id),
            'after_integral' => $after_integral,
            'form_order_id' => $form_order_id,
            'type' => $type
        ];
        try {
            $info = $this->model->insert($insert);
            $this->UserService->setIntegral($type, $integral, $user_id);
            return $this->format([], '添加成功');
        } catch (\Exception $e) {
            return $this->format_error('添加失败' . $e->getMessage());
        }

    }

    /**
     * 析构方法
     */
    public function __destruct()
    {
        $this->model;
        $this->UserService;
        // TODO: Implement __destruct() method.
    }

}
